package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.SysRoleMapper;
import com.bf.electroplating.mapper.SysUserRoleMapper;
import com.bf.electroplating.pojo.dto.IdDTO;
import com.bf.electroplating.pojo.dto.sysrole.RoleDetailDTO;
import com.bf.electroplating.pojo.dto.sysrole.RolePageDTO;
import com.bf.electroplating.pojo.entity.SysRole;
import com.bf.electroplating.pojo.entity.SysUserRole;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.SelectVo;
import com.bf.electroplating.pojo.vo.sysrole.RoleVO;
import com.bf.electroplating.service.ISysRoleFunctionService;
import com.bf.electroplating.service.ISysRoleService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * (角色表)SYS_ROLE 服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-10-18
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl < SysRoleMapper,SysRole > implements ISysRoleService {

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private ISysRoleFunctionService sysRoleFunctionService;

    @Override
    public ResultVO <List< SelectVo >> getRoleList() {
        List<SelectVo> selectVoList = new ArrayList<>();
        List<SysRole> sysRoleList;
            sysRoleList = new ArrayList<>(list());
        sysRoleList.forEach(i -> {
            SelectVo selectVo = new SelectVo();
            selectVo.setId(i.getId());
            selectVo.setName(i.getRoleName());
            selectVoList.add(selectVo);
        });
        return ResultVO.ok(selectVoList);
    }

    @Override
    public ResultVO<Page< RoleVO >> getRolePage( RolePageDTO dto) {

        Page<SysRole> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        Page<SysRole> sysRolePage = page(page, new LambdaQueryWrapper <SysRole> ()
                .like(StringUtils.isNotBlank(dto.getRoleName()), SysRole::getRoleName, dto.getRoleName())
                .orderByAsc(SysRole::getRoleName));

        Page<RoleVO> roleVOPage = new Page<>();
        BeanUtils.copyProperties(sysRolePage, roleVOPage);
        List<RoleVO> roleVOList = new ArrayList<>();
        sysRolePage.getRecords().forEach(i -> {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(i, roleVO);
            List< SysUserRole > sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>()
                    .eq(SysUserRole::getRId, i.getId()));
            roleVO.setPeopleNum(sysUserRoles.size());
            roleVOList.add(roleVO);
        });

        roleVOPage.setRecords(roleVOList);
        return ResultVO.ok(roleVOPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<Object> saveOrUpdateRole( RoleDetailDTO dto) {
        SysRole sysRoleCheck = getOne(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleName, dto.getRoleName()));
        if (dto.getId() == null) {
            if (sysRoleCheck != null){
                return ResultVO.fail("已存在该角色");
            }
        }else {
            if (sysRoleCheck != null && !sysRoleCheck.getId().equals(dto.getId())){
                return ResultVO.fail("已存在该角色");
            }
        }
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(dto, sysRole);
        return saveOrUpdate(sysRole) ? ResultVO.ok("保存成功") : ResultVO.fail("保存失败");
    }

    @Override
    public ResultVO<RoleDetailDTO> getDetailRole( IdDTO dto) {
        SysRole sysRole = getById(dto.getId());
        RoleDetailDTO roleDetailDTO = new RoleDetailDTO();
        BeanUtils.copyProperties(sysRole, roleDetailDTO);
        return ResultVO.ok(roleDetailDTO);
    }

    @Override
    public ResultVO<Object> deleteRole(IdDTO dto) {
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getRId, dto.getId()));
        if (!sysUserRoles.isEmpty()) {
            return ResultVO.fail("该角色下有用户，无法删除");
        }
        removeById(dto.getId());
        return ResultVO.ok(null, "删除成功");
    }
}
